<template>
  <div class="loginContainer">
    <el-card class="box-card">
      <div class="tittle"></div>
      <el-form ref="form" :model="user" :rules="rules">
        <el-form-item prop="name">
          <el-input prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="region">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            show-password
            v-model="user.password"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="btn" @click="login">登录按钮</el-button>
      <el-link :underline="false" @click="$router.push('/MyRegeister')">还没有注册，去登陆</el-link>
    </el-card>
  </div>
</template>

<script>
import { getLogin } from '@/api/user'
export default {
  name: 'MyLogin',
  components: {},
  data() {
    return {
      user: {
        username: '',
        password: ''
      },

      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  filters: {},
  watch: {},
  created() {},
  methods: {
    login() {
      this.$refs.form.validate(async vaild => {
        if (!vaild) return
        const { data: res } = await getLogin(this.user)
        if (res.code === 0) {
          // 成功：提示、存token、跳转到首页
          this.$message.success(res.message)
          // ????? 存储token
          // 跳转 ？？？？
          this.$store.commit('user/updateToken', res.token)
          this.$router.push('/')
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="css" scoped>
.loginContainer {
  background: url(../assets/images/login_bg.jpg) no-repeat;
  background-size: cover;
  height: 100%;
}
.el-card {
  width: 400px;
  /* height: 310px; */
  position: absolute;
  left: 50%;
  right: 50%;
  transform: translate(-50%, 100%);
}
.btn {
  width: 100%;
}
.tittle {
  height: 40px;
  background: url(@/assets/images/login_title.png) top center no-repeat;
}
</style>
